Skip to content

fix: resolve refs to definitions keys with percent-encoded characters - #871

Open
haoku123 wants to merge 1 commit into
fastify:mainfrom
haoku123:fix/percent-encoded-definitions-key
Open

fix: resolve refs to definitions keys with percent-encoded characters#871
haoku123 wants to merge 1 commit into
fastify:mainfrom
haoku123:fix/percent-encoded-definitions-key

Conversation

@haoku123

Copy link
Copy Markdown

Fixes #740.

Problem

When a definitions (or ``) key contains percent-encoded characters (e.g. 'Some%3Cloremipsum%3E') and its subschema uses `oneOf`/`allOf`/`anyOf`, building fails with `can't resolve reference` from ajv.

Root cause: the two resolvers disagree on how to interpret the fragment. ajv (per the JSON Schema spec) percent-decodes the fragment before looking up the definitions key (`unescapeFragment` = `decodeURIComponent` + JSON-pointer unescape), while the internal serializer resolves refs literally. A key copied verbatim into matches on the serializer side but misses on the ajv side.

Solution

In Validator.convertSchemaToAjvFormat, percent-decode the definitions/`` keys before handing the schema to ajv, so both resolvers agree:

  • invalid/incomplete percent sequences are decoded segment-by-segment, leaving invalid segments untouched
  • a key whose decoded form would collide with an existing key is left untouched
  • getState() now stores the converted schema, so the standalone-mode validator sees the same normalized keys

Tests

Added two tests in test/ref.test.js: a definitions key with %3C/%3E plus oneOf, and a `` variant. Full suite passes (493 tests), types (12 assertions) and eslint clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error using stringify on schema with uri-encoded definition property containing anyOf, allOf or oneOf

1 participant